package edu.zhku.ttms.service.impl;

import edu.zhku.ttms.domain.Car;
import edu.zhku.ttms.mapper.CarMapper;
import edu.zhku.ttms.service.CarService;
import edu.zhku.ttms.utils.MybatisUtil;
import edu.zhku.ttms.utils.StringUtil;
import edu.zhku.ttms.utils.SystemConst;

import java.util.List;

/**
 * @author 泽
 * @since 2021/4/19 8:31
 */


public class CarServiceImpl implements CarService {

    CarMapper carMapper = MybatisUtil.getMapper(CarMapper.class);

    @Override
    public Car getById(Long id) {
        return carMapper.getById(id);
    }

    @Override
    public List<Car> getList() {
        return carMapper.getList(null);
    }

    @Override
    public List<Car> getList(String str) {
        return carMapper.getList(StringUtil.ifEmpty2Null(str));
    }

    @Override
    public String save(Car car) {
        String licensePlateNumber = car.getLicensePlateNumber();
        if (StringUtil.isEmpty(licensePlateNumber)) {
            return "车牌号不能为空！";
        }
        if (!licensePlateNumber.matches("^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z][A-Z][A-Z0-9]{4}[A-Z0-9挂学警港澳]$")) {
            return "车牌号格式错误！";
        }
        String carDrivingLicenseNumber = car.getCarDrivingLicenseNumber();
        if (StringUtil.isEmpty(carDrivingLicenseNumber)) {
            return "车辆行驶证号不能为空！";
        }
        if (StringUtil.isEmpty(car.getOwnerName())) {
            return "车主姓名不能为空！";
        }
        if (carMapper.getByLicensePlateNumber(licensePlateNumber) != null) {
            return "车牌号为" + licensePlateNumber + "的车已存在！";
        }
        if (carMapper.getByLicensePlateNumber(licensePlateNumber) != null) {
            return "车牌号为" + licensePlateNumber + "的车已存在！";
        }
        if (carMapper.insertOne(car) > 0) {
            return SystemConst.SUCCESS;
        }
        return "操作失败！";

    }

    @Override
    public String modify(Car car) {
        String licensePlateNumber = car.getLicensePlateNumber();
        if (StringUtil.isEmpty(licensePlateNumber)) {
            return "车牌号不能为空！";
        }
        if (!licensePlateNumber.matches("^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z][A-Z][A-Z0-9]{4}[A-Z0-9挂学警港澳]$")) {
            return "车牌号格式错误！";
        }
        String carDrivingLicenseNumber = car.getCarDrivingLicenseNumber();
        if (StringUtil.isEmpty(carDrivingLicenseNumber)) {
            return "车辆行驶证号不能为空！";
        }
        if (StringUtil.isEmpty(car.getOwnerName())) {
            return "车主姓名不能为空！";
        }

        Car originalCar = carMapper.getById(car.getCarId());
        if (originalCar.getLicensePlateNumber().equals(licensePlateNumber)) {
            car.setLicensePlateNumber(null);
        } else if (carMapper.getByLicensePlateNumber(licensePlateNumber) != null) {
            return "车牌号为" + licensePlateNumber + "的车已存在！";
        }
        if (originalCar.getCarDrivingLicenseNumber().equals(carDrivingLicenseNumber)) {
            car.setCarDrivingLicenseNumber(null);
        } else if (carMapper.getByLicensePlateNumber(licensePlateNumber) != null) {
            return "车牌号为" + licensePlateNumber + "的车已存在！";
        }
        if (carMapper.updateOne(car) > 0) {
            return SystemConst.SUCCESS;
        }
        return "操作失败！";
    }

    @Override
    public int removeById(Long carId) {
        return carMapper.removeById(carId);
    }

    @Override
    public List<String> getLicensePlateNumberList() {
        return carMapper.getLicensePlateNumberList();
    }

    @Override
    public Car getByLicensePlateNumber(String licensePlateNumber) {
        return carMapper.getByLicensePlateNumber(licensePlateNumber);
    }

    @Override
    public Car getByCarDrivingLicenseNumber(String carDrivingLicenseNumber) {
        return carMapper.getByCarDrivingLicenseNumber(carDrivingLicenseNumber);
    }
}
